package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.VO.IdCardVO;
import com.source.lightfowl.basedata.dao.IdCardDao;
import com.source.lightfowl.basedata.entity.IdCard;
import com.source.lightfowl.basedata.service.IdCardService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.utils.StringTOArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 13:05
 */
@Service
public class IdCardServiceImpl implements IdCardService {

    @Autowired
    private IdCardDao idCardDao;
    @Autowired
    private PersonDao personDao;

    @Override
    public List<IdCardVO> findAll() {
        List<IdCard> list = idCardDao.findAll();
        List<IdCardVO> collect = list.stream().map(s -> {
            IdCardVO vo = new IdCardVO();
            BeanUtils.copyProperties(s, vo);


            Person user = personDao.findVO(s.getCreatBy());
            vo.setUserName(user.getName());



            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Page<IdCard> findAllPage(int page, int pageNum) {
        return idCardDao.findAll(PageRequest.of(page-1,pageNum));
    }

    @Override
    public Optional<IdCard> findById(Integer id) {
        return idCardDao.findById(id);
    }

    @Override
    public void save(IdCard sh, HttpServletRequest request) {
        /*//设置创建时间
        if(sh.getCreateDate() == null){
            sh.setCreateDate(LocalDateTime.now().toString());
        }

        HttpSession session = request.getSession();
        Person user =(Person) session.getAttribute("person");
        Person person = personDao.checkUsername(user.getLoginName());
        if (person != null){
            sh.setCreatBy(person.getId().toString());

        }

        idCardDao.save(sh);*/

        //设置创建时间
        if(sh.getCreateDate() == null){
            sh.setCreateDate(LocalDateTime.now().toString());
        }

        HttpSession session = request.getSession();
        Person user =(Person) session.getAttribute("person");
        if(user != null){
            Person person = personDao.checkUsername(user.getLoginName());
            if (person != null){
                sh.setCreatBy(person.getId().toString());

            }

            idCardDao.save(sh);
        }
    }

    @Override
    public void deleteById(Integer id) {
        idCardDao.deleteById(id);
    }

    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        idCardDao.deleteBatch(list);
    }
}
